package panel.mapPanel
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	
	import panel.viewSpots.navigation.ScrollTxtHor;
	
	public class TimeLineInfoBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var background:Shape;
		private var titleText:ScrollTxtHor;
		private var descText:TextField;
		private var img:ImageResource;
		private var arrow:Shape;
		private var floatIcon:FloatIcon;
		
		private var _onRepo:Function;
		
		public var info:*;
		
		public function TimeLineInfoBox(stage:Stage,loader:ResourceLoader,lang:Lang,uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		private function initThis():void
		{
			background = new Shape;
			background.graphics.beginFill(0x000000,.5);
			background.graphics.drawRoundRect(0,0,_stage.stageWidth * .7,100,5);
			background.graphics.endFill();
			
			arrow = new Shape;
			arrow.graphics.beginFill(0x000000,.5);
			arrow.graphics.drawTriangles(Vector.<Number>([0,_stage.stageWidth * .05, _stage.stageWidth * .05,0, _stage.stageWidth * .05,_stage.stageWidth * .1]));
			arrow.graphics.endFill();
			
			titleText = new ScrollTxtHor;
			titleText.mouseEnabled = false;
			titleText.showWid = _stage.stageWidth * .6;
			
//			titleText.multiline = false;
//			titleText.wordWrap = false;
			
			descText = new TextField;
			descText.mouseEnabled = false;
			descText.multiline = true;
			descText.wordWrap = true;
		}
		
		public function changeData(obj:*):void
		{
			if(obj){
				
				if(obj is String){
					if(obj.split("^")[1] == "start"){
						
						background = null;
						arrow = null;
						
						background = new Shape;
						background.graphics.beginFill(0xFF9900,.8);
						background.graphics.drawRoundRect(0,0,_stage.stageWidth * .7,_stage.stageWidth * .08,5);
						background.graphics.endFill();
						
						arrow = new Shape;
						arrow.graphics.beginFill(0xFF9900,.8);
						arrow.graphics.drawTriangles(Vector.<Number>([0,_stage.stageWidth * .04, _stage.stageWidth * .04,0, _stage.stageWidth * .04,_stage.stageWidth * .08]));
						arrow.graphics.endFill();
						
						this.addChild(background);
						this.addChild(arrow);
						this.addChild(titleText);
						
						background.x = _stage.stageWidth * .25;
						background.y = 0;
						arrow.x = background.x - arrow.width;
						
//						TextFieldUtils.setHtmlText(titleText,HtmlText.formatSizeColor(obj.split("^")[0],0xFFFF00,20),true,true);
//						titleText.width = titleText.textWidth + 5;
//						titleText.height = titleText.textHeight + 5;
						titleText.label(HtmlText.formatSizeColor(obj.split("^")[0],0xFFFF00,20),0xFFFF00,20,true);
						titleText.x = background.x + _stage.stageWidth * .02;
						titleText.y = background.y + 5;
						
						floatIcon = new FloatIcon(_stage,_loader,_uri);
						floatIcon.changeData(obj.split("^")[1]);
						this.addChild(floatIcon);
						
						reposition();
						
					}if(obj.split("^")[1] == "end"){
						
						background = null;
						arrow = null;
						
						background = new Shape;
						background.graphics.beginFill(0xFF9900,0);
						background.graphics.drawRoundRect(0,0,_stage.stageWidth * .7,_stage.stageWidth * .05,5);
						background.graphics.endFill();
						this.addChild(background);
						
						arrow = new Shape;
						arrow.graphics.beginFill(0xFF9900,0);
						arrow.graphics.drawTriangles(Vector.<Number>([0,_stage.stageWidth * .05, _stage.stageWidth * .04,0, _stage.stageWidth * .04,_stage.stageWidth * .05]));
						arrow.graphics.endFill();
						this.addChild(arrow);
						
						floatIcon = new FloatIcon(_stage,_loader,_uri);
						floatIcon.changeData(obj.split("^")[1]);
						this.addChild(floatIcon);
						
						reposition();
					}
					
				}else{
					info = obj;
					
					this.addChild(background);
					this.addChild(arrow);
					this.addChild(titleText);
					this.addChild(descText);
					
					background.x = _stage.stageWidth * .25;
					background.y = 0;
					arrow.x = background.x - arrow.width;
					
//					TextFieldUtils.setHtmlText(titleText,HtmlText.formatSizeColor(obj.name,0xFFFF00,20));
//					titleText.width = titleText.textWidth + 5;
//					titleText.height = titleText.textHeight + 5;
					titleText.label(HtmlText.formatSizeColor(obj.name,0xFFFF00,20));
					titleText.x = background.x + _stage.stageWidth * .02;
					titleText.y = background.y + 5;
					
					clearImg();
					img = new ImageResource(_loader,obj.url,reposition);
					this.addChild(img);
					img.x = background.x + 5
					img.y = titleText.y + titleText.height + 5;
					
					floatIcon = new FloatIcon(_stage,_loader,_uri);
					floatIcon.changeData(obj);
					this.addChild(floatIcon);
				}
			}
		}
		
		private function clearImg():void
		{
			if(img != null){
				this.removeChild(img);
				img = null;
			}
		}
		
		private function reposition():void
		{
			if(_stage){
				if(info){
					if(img.width > _stage.stageWidth * .7 - 10){
						img.width = _stage.stageWidth * .7 - 10;
					}
					if(img.height > _stage.stageWidth * .7 - 10){
						img.height = _stage.stageWidth * .7 - 10;
					}
					
					TextFieldUtils.setHtmlText(descText,HtmlText.formatSizeColor(info.desc,0xFFFFFF));
					descText.width = background.width - _stage.stageWidth * .04;
					descText.height = descText.textHeight + 5;
					descText.x = background.x + _stage.stageWidth * .02;
					descText.y = img.y + img.height + 5;
					
					background.height = descText.y + descText.height + 5;
				}
				
				arrow.y = (background.height - arrow.height) / 2 + background.y;
				
				floatIcon.x = _stage.stageWidth * .1 - floatIcon.width / 2;
				floatIcon.y = (background.height - floatIcon.height) / 2 + background.y;
				
				if(_onRepo is Function){
					_onRepo(this);
				}
			}
		}
		
		public function set repo(func:Function):void
		{
			_onRepo = func;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				if(background){
					background = null;
				}if(titleText){
					titleText = null;
				}if(descText){
					descText = null;
				}if(img){
					img = null;
				}if(arrow){
					arrow = null;
				}if(floatIcon){
					floatIcon = null;
				}
			}
		}
	}
}